<template>
  <el-dialog :visible.sync="dialogVisible" :title="title" width="500px" @callBack="onCallBack" @close="onClose">
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px" label-position="left">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" clearable placeholder="请输入名称"/>
      </el-form-item>

      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="form.type">
          <el-radio :label="1">短信模板</el-radio>
          <el-radio :label="2">邮件模板</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="模块" prop="fromClass">
        <el-select v-model="form.fromClass" placeholder="请选择模块" style="width: 100%" @change="onChange">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>

  </el-dialog>
</template>

<script>
import { getOne, save, onSelectModel } from '@/api/sysTemplate'

export default {
  data() {
    return {
      dialogVisible: false,
      title: '添加',
      form: {
        id: '',
        name: '',
        type: '',
        fromClass: '',
        fromClassName: ''
      },
      options: [],
      rules: {
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          }
        ],
        type: [
          {
            required: true,
            message: '请选择类型',
            trigger: 'blur'
          }
        ],
        fromClass: [
          {
            required: true,
            message: '请选择模块信息',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    init() {
      if (this.form.id) {
        getOne(this.form.id).then((res) => {
          if (res && res.code === 20000) {
            const rtn = res.data
            this.form.id = rtn.id
            this.form.name = rtn.name
            this.form.type = rtn.type
            this.form.fromClass = rtn.fromClass
            this.form.context = rtn.context
          }
        })
      }
    },
    onShow(id) {
      if (id) {
        this.form.id = id
        this.title = '编辑'
        this.init()
      } else {
        this.title = '添加'
      }
      this.dialogVisible = true
    },
    onClose() {
      this.$refs['ruleForm'].resetFields()
      this.form = this.$options.data().form
      this.dialogVisible = false
    },
    onChange(val) {
      this.options.forEach((item, index) => {
        if (item.value === val) {
          this.form.fromClassName = item.label
        }
      })
    },
    onSubmit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          save(this.form)
            .then((res) => {
              if (res && res.code === 20000) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                this.dialogVisible = false
                this.onCallBack()
              }
            })
            .catch((e) => {
            })
        } else {
          return false
        }
      })
    },
    onQueryModel() {
      onSelectModel().then(res => {
        if (res.code === 20000) {
          this.options = res.data
        }
      })
    },
    onCallBack() {
      this.$emit('onCallBack')
    }
  },
  mounted() {
    this.onQueryModel()
  }
}
</script>

<style scoped>
.el-form {
  padding-left: 60px;
  padding-right: 60px;
}

</style>
